<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/17
 * Time: 12:46
 */

namespace backend\gen;
use console\controllers\GenController;
use Yii;
use yii\helpers\Inflector ;

class CurdGenerator
{
    use DbGenerator;
    /**
     * @var GenController
     */
    static $console = null ;
    static $colmns = [] ;
    public static function build(GenController $console){
        self::$console = $console ;
        $model = $console->model ;
        $tableName = $model::tableName() ;
        $colmns = Yii::$app->db->createCommand("SHOW FULL COLUMNS FROM `{$tableName}`")->query()->readAll();
        self::$colmns = $colmns ;

        //self::$colmns = self::formatColmns($colmns) ;
        self::buildCreate();
        self::buildController();
        self::buildIndex();
    }


    public static function buildIndex(){
        $colmns = self::$colmns ;
        $className = self::$console->className;


        $classNameArr = explode('/',$className) ;
        $trueClassName = array_pop($classNameArr) ;

        if(!empty($classNameArr)){
            $classNameID = implode('/',$classNameArr).'/'.Inflector::camel2id($trueClassName) ;
        }else{
            $classNameID = Inflector::camel2id($trueClassName) ;
        }


        $view = self::$console->view;

        ob_start();
        include __DIR__ . '/stub/index.stub.php';
        $content = ob_get_contents();
        ob_end_clean();
        $filename = Yii::getAlias("@backend/views/{$view}/index.php");
        self::checkPath($filename);
        file_put_contents($filename,$content) ;
    }

    public static function buildController(){
        $className = self::$console->className ;
        $classNameArr = explode('/',$className) ;
        $trueClassName = array_pop($classNameArr) ;
        $subControllerNameSpace = implode('\\',$classNameArr);
        if($subControllerNameSpace){
            $subControllerNameSpace = '\\'.$subControllerNameSpace ;
        }

        $model = self::$console->model;
        $modelArr = explode('\\',$model) ;
        $trueModelName = array_pop($modelArr) ;
        $subNameSpace = str_replace('/','\\',$model);

        $view = self::$console->view;
        $viewSpace = str_replace('/','.',$view) ;

        ob_start();
        include __DIR__ . '/stub/contrller.stub.php';
        $content = ob_get_contents();
        ob_end_clean();

        $filename = Yii::getAlias("@backend/controllers/{$className}Controller.php");
        self::checkPath($filename);
        file_put_contents($filename,$content) ;
    }

    public static function buildCreate(){

        $colmns = self::$colmns ;
        $view = self::$console->view;

        ob_start();
        include __DIR__ . '/stub/create.stub.php';
        $content = ob_get_contents();
        ob_end_clean();
        $filename = Yii::getAlias('backend/views/'.$view.'/save.php') ;
        self::checkPath($filename);
        file_put_contents($filename,$content) ;
    }

    public static function checkPath($path){
        $dirName = dirname($path) ;
        if(!file_exists($dirName)){
            mkdir($dirName,0777,true);
        }
    }




}